from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.apis.router import router
from app.common.exception.exception_handler import register_excetpion_handler
from app.database.db import create_tables
from app.utils.serializers import MsgSpecJSONResponse


@asynccontextmanager
async def lifespan(app: FastAPI):
    await create_tables()
    yield


def register_app():
    app = FastAPI(
        title="Demo",
        default_response_class=MsgSpecJSONResponse,
        lifespan=lifespan,
    )

    app.add_middleware(CORSMiddleware)
    register_excetpion_handler(app=app)
    app.include_router(router)

    return app
